//
//  MineCreateOpusTypeHeaderView.m
//  ShowYork
//
//  Created by hxh on 2021/3/8.
//  Copyright © 2021 hxh. All rights reserved.
//

#import "MineCreateOpusTypeHeaderView.h"
#import "MineCreateOpusTypeItemCell.h"
#import "LessonSearchFlowLayout.h"


@interface MineCreateOpusTypeHeaderView ()<UICollectionViewDelegate,UICollectionViewDataSource,LessonSearchFlowLayoutDelegate,MineCreateOpusTypeItemCellDelegate>
@property (nonatomic,strong)UILabel *titleLb;
@property (nonatomic,strong)UIImageView *titleIcon;
@property (nonatomic,strong)UICollectionView *collectionView;

@property (nonatomic,strong)SocialSubTagModel *lastModel;

@end


@implementation MineCreateOpusTypeHeaderView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setUI];
    }
    return self;
}

- (void)setUI{
    self.titleIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mine_opus_type_tag"]];
    [self addSubview:self.titleIcon];
    [self.titleIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.left.equalTo(self).offset(16);
    }];
    self.titleLb = [[UILabel alloc]init];
    self.titleLb.text = @"作品分类";
    self.titleLb.textColor = C51;
    self.titleLb.font = font(14);
    [self addSubview:self.titleLb];
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.titleIcon);
        make.left.equalTo(self.titleIcon.mas_right).offset(10);
    }];
    
    LessonSearchFlowLayout *layout = [LessonSearchFlowLayout new];

    layout.labelFont = [UIFont systemFontOfSize:12];
    //自定义layout初始化
    layout.delegate = self;
    layout.lineSpacing = 8.0;
    layout.interitemSpacing = 16.0f;
    layout.headerViewHeight = 0;
    layout.footerViewHeight = 0;
    layout.itemHeight = 26.0f;
    layout.itemInset = UIEdgeInsetsMake(0, 8, 0, 8);
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    [self.collectionView registerClass:[MineCreateOpusTypeItemCell class] forCellWithReuseIdentifier:@"MineCreateOpusTypeItemCell"];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleIcon.mas_bottom).offset(16);
        make.left.right.bottom.equalTo(self);
    }];
    self.collectionView.backgroundColor = [UIColor whiteColor];
}

- (void)setDataArray:(NSMutableArray *)dataArray{
    
    _dataArray = dataArray;

    [self.collectionView reloadData];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MineCreateOpusTypeItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MineCreateOpusTypeItemCell" forIndexPath:indexPath];
    [cell setItemModel:self.dataArray[indexPath.item]];
    cell.delegate = self;
    return cell;
}
- (NSString *)collectionViewItemSizeWithIndexPath:(NSIndexPath *)indexPath{
    
    SocialSubTagModel *itemModel = self.dataArray[indexPath.item];
    
    return itemModel.tagName;
}

#pragma mark - MineCreateOpusTypeItemCellDelegate
- (void)updateOpusStautsWithCurrentItem:(SocialSubTagModel *)itemModel{
    if (![self.lastModel.id isEqualToNumber:itemModel.id]) {
        self.lastModel.isSelect = NO;
    }
    self.lastModel = itemModel;
    
    [self.collectionView reloadData];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(getcurrentOpusTagItem:)]) {
        [self.delegate getcurrentOpusTagItem:itemModel];
    }
    
}

@end
